#include "curl.h"
#include <stdio.h>
#include  "cJSON.h"

size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    printf("%s\n",result);

    cJSON *jsonstr = cJSON_Parse(result);
    if(jsonstr!=NULL){
        cJSON *successRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"success");
        cJSON *codeRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"code");
        cJSON *messageRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"message");

        if(cJSON_IsTrue(successRoot)){
            printf("%s\n",messageRoot->valuestring);
        }else{
            perror("错误");
        }
    }

    return realsize;
}

int main(int argc, char const *argv[])
{
    CURL *curl = curl_easy_init();

    curl_easy_setopt(curl,CURLOPT_URL,"http://47.109.65.208/config.json");

    //curl_easy_setopt(curl,CURLOPT_POSTFIELDS,"");
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeCallback);

    CURLcode code = curl_easy_perform(curl);
    if(code != CURLE_OK )
    {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(code));
    }

    curl_easy_cleanup(curl);

    return 0;
}
